﻿using ImageDefine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageTaskModule
{
    [Serializable]
    public class ImageTaskCenter
    {
        public BindingList<ImageTask> listTasks;
        [NonSerialized]
        public Dictionary<string, ImageTask> dicTasks;
        [NonSerialized]
        public string strSettingPath;
        
        public ImageTaskCenter()
        {
            listTasks = new BindingList<ImageTask>();
            dicTasks = new Dictionary<string, ImageTask>();
        }
        public static ImageTaskCenter Load(string strPath)
        {
            ImageTaskCenter pDoc = null;
            //XmlSerializer xmlSerializer = new XmlSerializer(typeof(ImageTaskCenter));
            BinaryFormatter binSerializer = new BinaryFormatter();
            FileStream fsReader = null;
            try
            {
                fsReader = File.OpenRead(strPath + @"/ImageTaskCenter" + ".bat");
                pDoc = (ImageTaskCenter)binSerializer.Deserialize(fsReader);
                fsReader.Close();
                pDoc.strSettingPath=strPath;
                pDoc.dicTasks = pDoc.listTasks.ToDictionary(p => p.Name);
                foreach (ImageTask itemTask in pDoc.listTasks)
                {
                    itemTask.strPath = strPath;
                    itemTask.dicTools = itemTask.listTools.ToDictionary(p => p.strName);
                    itemTask.InputParameters = new Dictionary<string, string>();
                    foreach (ImageDefine.ImageToolBase itemTool in itemTask.listTools)
                    {
                        itemTool.CreateActioner(itemTask);
                    }
                }
            }
            catch (IOException)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new ImageTaskCenter();
                pDoc.strSettingPath = strPath;
                pDoc.dicTasks = pDoc.listTasks.ToDictionary(p => p.Name);
                pDoc.Save();
            }
            catch (Exception)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new ImageTaskCenter();
                pDoc.strSettingPath = strPath;
                pDoc.dicTasks = pDoc.listTasks.ToDictionary(p => p.Name);
                pDoc.Save();
            }
            return pDoc;
        }
        public bool Save()
        {
            if (!Directory.Exists(strSettingPath))
            {
                Directory.CreateDirectory(strSettingPath);
            }
            FileStream fsWriter = new FileStream(strSettingPath + @"/ImageTaskCenter" + ".bat", FileMode.Create, FileAccess.Write, FileShare.Read);
            
            BinaryFormatter binSerializer = new BinaryFormatter();
            binSerializer.Serialize(fsWriter, this);
            fsWriter.Flush(true);
            fsWriter.Close();


           


            return true;
        }
        public void Add(ImageTask newTask)
        {
            newTask.strPath = strSettingPath;
            listTasks.Add(newTask);
            dicTasks = listTasks.ToDictionary(p => p.Name);

        }
        public void Insert(int index, ImageTask newTask)
        {
            newTask.strPath = strSettingPath;
            listTasks.Insert(index, newTask);
            dicTasks = listTasks.ToDictionary(p => p.Name);

        }
        public void Remove(int index)
        {
            listTasks.RemoveAt(index);
            dicTasks = listTasks.ToDictionary(p => p.Name);
            //listTasks.Reverse(index,2);

        }
        public void Modify(int index, ImageTask newTask)
        {
            listTasks[index].Name = newTask.Name;
            dicTasks = listTasks.ToDictionary(p => p.Name);

            foreach (ImageDefine.ImageToolBase itemTool in newTask.listTools)
            {
                itemTool.strTaskName= newTask.Name;
            }
        }

    }
}
